﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ewk.MachineLearning.Persistance.Domain;
using Ewk.MachineLearning.Persistance.Interfaces;

namespace Ewk.MachineLearning.Persistance.DataAccess
{
    public class EntityRepository<T> where T : Entity
    {
        public EntityRepository(ICatalog catalog)
        {
            Catalog = catalog;
        }

        public T Add(T entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");

            return Catalog.Add(entity);
        }

        public T Update(T entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");

            return Catalog.Update(entity);
        }

        public void Delete(T entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");

            Catalog.Remove(entity);
        }

        public T GetEntityById(Guid id)
        {
            return Catalog.Entities<T>()
                .SingleOrDefault(entity => entity.Id == id);
        }

        public IEnumerable<T> Entities
        {
            get { return Catalog.Entities<T>(); }
        }

        public void DeleteAll()
        {
            Catalog.RemoveAll<T>();
        }

        protected ICatalog Catalog { get; private set; }
    }
}